﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Projekt
{
    public partial class firm_view : Form
    {
        private ToolTip tt = new ToolTip();
        private DataTable firmNames;
        private String firmNameInputErrorMsg = "";
        private String firmPhoneNumberInputErrorMsg = "";
        
        public firm_view()
        {
            InitializeComponent();
        }

        /*  ---------------------------------------------
         *  |           Input validating:               |   
         *  |FirmName:                                  |
         *  |   not exist in database                   |   
         *  |shopPhoneNumber:                           |
         *  |   10 or more digit                        |
         *  |   only numeric character between 0 and 9  |
         *  ---------------------------------------------
         *  */
        
        private void firmName_Validating(object sender, CancelEventArgs e)
        {
            firmNameInputErrorMsg = "";
            if (nameEdit.Text == "") 
            { 
                firmNameInputErrorMsg = firmNameInputErrorMsg + "Invalid name";
                inputErrorImage.Visible = true;
            }
            else
            {
                foreach (DataRow rows in firmNames.Rows)
                {
                    if (rows["CNev"].ToString() == nameEdit.Text.ToString() && rows["CegID"].ToString() != cuiEdit.Text.ToString())
                    {
                        firmNameInputErrorMsg = firmNameInputErrorMsg + "This firm already exist";
                        inputErrorImage.Visible = true;
                    }
                }   
            }
        }
        private void firmName_Validated(object sender, EventArgs e)
        {
            try
            {
                if (firmNameInputErrorMsg == "")
                {
                    if (firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == "")
                    {
                        Save.Enabled = true;
                        inputErrorImage.Visible = false;
                    }
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
            finally { Save.Enabled = firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == "";}
        }
        private void firmPhoneNumber_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                firmPhoneNumberInputErrorMsg = "";
                if (numberEdit.Text.ToString().Length > 0)
                {
                    Int64 numberEntered = Int64.Parse(numberEdit.Text);
                    if (numberEdit.Text.ToString().Length < 10)
                    {
                        firmPhoneNumberInputErrorMsg = firmPhoneNumberInputErrorMsg + "Phone number is 10 or more digit\n";
                        inputErrorImage.Visible = true;
                    }
                }
            }
            catch (FormatException)
            {
                firmPhoneNumberInputErrorMsg = firmPhoneNumberInputErrorMsg + "Phone number contains only numeric digits";
                inputErrorImage.Visible = true;
            }
        }
        private void firmPhoneNumber_Validated(object sender, EventArgs e)
        {
            try
            {
                if (firmPhoneNumberInputErrorMsg == "")
                {
                    if (firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == "")
                    {
                        Save.Enabled = true;
                        inputErrorImage.Visible = false;
                    }
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
            finally { Save.Enabled = firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == ""; }   
        }

        private void firm_listview_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                CUI_input.Visible = false;
                Name_input.Visible = false;
                CUI_label.Visible = false;
                Name_label.Visible = false;
                nameEdit.Visible = true;
                numberEdit.Visible = true;
                addressEdit.Visible = true;
                Save.Visible = true;
                Cancel.Visible = true;
                ListViewItem item = firm_viewlist.FocusedItem;
                cuiEdit.Text = item.SubItems[0].Text;
                nameEdit.Text = item.SubItems[1].Text;
                numberEdit.Text = item.SubItems[2].Text.Trim();
                addressEdit.Text = item.SubItems[3].Text;

            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Firm ceg = new Firm();
                ListViewItem item = firm_viewlist.FocusedItem;
                String Message = "Do you wan to delete " + item.SubItems[1].Text;
                DialogResult result = MessageBox.Show(Message, "Delete", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    if (ceg.DeleteFirm(item.SubItems[0].Text)) firm_viewlist.Items.Remove(item);
                    else MessageBox.Show("This firm can't be deleted because it is in use\n At first, you need to delete the bills from this firm");
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void CUI_input_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Firm ceg = new Firm();
                firm_viewlist.Items.Clear();
                DataTable dt;
                if (CUI_input.Text == "" && Name_input.Text == "") dt = ceg.getFirmList();
                else dt = ceg.SearchFirm(CUI_input.Text, Name_input.Text);
                foreach (DataRow rows in dt.Rows)
                {
                    ListViewItem it = new ListViewItem(rows["CegID"].ToString());
                    it.SubItems.Add(rows["CNev"].ToString());
                    it.SubItems.Add(rows["CTel"].ToString());
                    it.SubItems.Add(rows["CCim"].ToString());

                    firm_viewlist.Items.Add(it);
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void Name_input_TextChanged(object sender, EventArgs e)
        {
            CUI_input_TextChanged(sender, e);
        }

        private void firm_view_Load(object sender, EventArgs e)
        {
            try
            {
                firm_viewlist.Items.Clear();
                Firm ceg = new Firm();
                DataTable dt = ceg.getFirmList();
                firmNames = dt;
                foreach (DataRow rows in dt.Rows)
                {
                    ListViewItem it = new ListViewItem(rows["CegID"].ToString());
                    it.SubItems.Add(rows["CNev"].ToString());
                    it.SubItems.Add(rows["CTel"].ToString());
                    it.SubItems.Add(rows["CCim"].ToString());
                    
                    firm_viewlist.Items.Add(it);
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                Firm ceg = new Firm();
                ListViewItem item = firm_viewlist.FocusedItem;
                ceg.EditFirm(item.SubItems[0].Text.ToString(), nameEdit.Text.ToString(), numberEdit.Text.ToString(), addressEdit.Text.ToString());
                CUI_input.Visible = true;
                Name_input.Visible = true;
                CUI_label.Visible = true;
                Name_label.Visible = true;
                nameEdit.Visible = false;
                numberEdit.Visible = false;
                addressEdit.Visible = false;
                Save.Visible = false;
                Cancel.Visible = false;
                inputErrorImage.Visible = false;
                firm_view_Load(sender,e);
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            try
            {
                CUI_input.Visible = true;
                Name_input.Visible = true;
                CUI_label.Visible = true;
                Name_label.Visible = true;
                nameEdit.Visible = false;
                numberEdit.Visible = false;
                addressEdit.Visible = false;
                Save.Visible = false;
                Cancel.Visible = false;
                inputErrorImage.Visible = false;
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void nameEdit_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void numberEdit_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void nameInputWarning_MouseOver(object sender, EventArgs e)
        {
            try
            {
                String ttString = "";
                if (firmNameInputErrorMsg != "") ttString = ttString + "Name: " + firmNameInputErrorMsg + "\n";
                if (firmPhoneNumberInputErrorMsg != "") ttString = ttString + "Phone number: " + firmPhoneNumberInputErrorMsg + "\n";
                tt.Show(ttString, (PictureBox)sender);
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void tooltpit_reset(object sender, EventArgs e)
        {
            tt.RemoveAll();
        }

    }
}
